package com.he.architecture.controller;

import com.he.architecture.Enums.CustomerOrderTypeEnums;
import com.he.architecture.common.controller.LogicController;
import com.he.architecture.dao.CustomerDao;
import com.he.architecture.dto.CreateOrderRequestDto;
import com.he.architecture.dto.EntityDtos.CustomerOrderResponse;
import com.he.architecture.dto.EntityDtos.ItemResponse;
import com.he.architecture.dto.EntityDtos.ShopItemResponse;
import com.he.architecture.dto.ResponseResult;
import com.he.architecture.dto.UpdateOrderRequestDto;
import com.he.architecture.entity.Customer;
import com.he.architecture.entity.CustomerOrder;
import com.he.architecture.entity.Item;
import com.he.architecture.service.CustomerService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/customer")
public class CustomerController extends LogicController<CustomerService, CustomerDao, Customer,Long> {
    /** DTO转化器 */
    @Resource
    private ModelMapper modelMapper;
    public CustomerController(@Autowired CustomerService service){
        super(service);
    }
    @PostMapping("/createOrder")
    public ResponseResult formOrder(@RequestBody CreateOrderRequestDto orderRequestDto){
        CustomerOrder customerOrder= getService().formOrder(orderRequestDto.getCus_id(),orderRequestDto.getShop_id(),orderRequestDto.getCustomerOrder());
        CustomerOrderResponse orderResponse=modelMapper.map(customerOrder,CustomerOrderResponse.class);
        Map<String,Object> map=new HashMap<>();
        map.put("orderResponse",orderResponse);
        return new ResponseResult(200,map);
    }
    @PostMapping("/updateOrder")
    public ResponseResult updateOrder(@RequestBody UpdateOrderRequestDto orderRequestDto){
        Set<CustomerOrder> customerOrders= getService().customerOrderTypeChange(orderRequestDto.getCus_id(),orderRequestDto.getShop_id(),orderRequestDto.getCustomerOrder(), CustomerOrderTypeEnums.valueOf(orderRequestDto.getOperationType()));
        Set<CustomerOrderResponse> customerOrderResponses=customerOrders.stream().map(customerOrder->modelMapper.map(customerOrder,CustomerOrderResponse.class)).collect(Collectors.toSet());
        Map<String,Object> map=new HashMap<>();
        map.put("customerOrderResponses",customerOrderResponses);
        return new ResponseResult(200,map);
    }
}

